/*********************************************************************************
 * Software License Agreement (BSD License)
 * Copyright (c) 2012, Carlos D. Correa (info@ngraph.org)
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of netzen nor the
 *    names of its contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *********************************************************************************/
#include "comboBoxDelegate.h"
#include <stdio.h>
#include <QComboBox>
#include <string>
#include "visualMapping.h"
using namespace std;


ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
     : QItemDelegate(parent)
 {
      printf("item delegate create\n");
 }

void ComboBoxDelegate::setStringModel(int i, QAbstractItemModel *model) {
  if(i>=listModels.size()) 
    listModels.push_back(model);
  else {
    listModels[i] = model;
  }
}

QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
     const QStyleOptionViewItem & option,
     const QModelIndex & index) const
 {
   if(index.column()==1) {
     QModelIndex typeIdx = index.sibling(index.row(), 2);
     int type = typeIdx.data().toInt();
     int idx = type == NODE_PROPERTY? 0: type==EDGE_PROPERTY?1: type==COLORMAP? 2:-1;
     if(idx==-1) return QItemDelegate::createEditor(parent, option, index);

     //printf("CreateEditor for column %d\n", index.column());
     QComboBox *editor = new QComboBox(parent);
     editor->setModel(listModels[idx]);
     connect(editor, SIGNAL(currentIndexChanged(int)), SLOT(changed(int)));
     return editor;
   } else {
     return QItemDelegate::createEditor(parent, option, index);
   }
     
 }

void ComboBoxDelegate::changed(int) {
  QWidget *editor = qobject_cast<QWidget *>(sender());
  commitData(editor);
}

void ComboBoxDelegate::setEditorData(QWidget *editor,
                                     const QModelIndex &index) const
 {
    if(index.column()==1) {
      //printf("setEditorData for column %d value :", index.column());
      QModelIndex typeIdx = index.sibling(index.row(), 2);
      int type = typeIdx.data().toInt();
      //printf("Type for row %d is %d\n", index.row(), type);
      int idx = type == NODE_PROPERTY? 0: type==EDGE_PROPERTY?1: type==COLORMAP? 2:-1;
      if(idx==-1)  {
	QItemDelegate::setEditorData(editor, index);
	return;
      }
  
      QString qstr = (index.data().toString());
      string str = qstr.toStdString();
      QComboBox *combo = static_cast<QComboBox*>(editor);

      QStringList list = listModels[idx]->stringList();
      int index = list.indexOf(qstr);
      combo->setCurrentIndex(index);
    } else {
      QItemDelegate::setEditorData(editor, index);
    }

 }

void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                    const QModelIndex &index) const
 {
   if(index.column()==1) {
     // printf("setModelData for column %d\n", index.column());
     QModelIndex typeIdx = index.sibling(index.row(), 2);
     int type = typeIdx.data().toInt();
     int idx = type == NODE_PROPERTY? 0: type==EDGE_PROPERTY?1: type==COLORMAP? 2:-1;
     if(idx==-1)  {
       QItemDelegate::setModelData(editor, model, index);
       return;
     }
     QComboBox *comboBox = static_cast<QComboBox*>(editor);
     int value = comboBox->currentIndex();
     model->setData(index, listModels[idx]->data(listModels[idx]->index(value), Qt::EditRole), Qt::EditRole);
   } else {
     QItemDelegate::setModelData(editor, model, index);
   }
 }

void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
     const QStyleOptionViewItem &option, const QModelIndex & index ) const
 {
   if(index.column()==1) {
     editor->setGeometry(option.rect);
   } else {
     QItemDelegate::updateEditorGeometry(editor, option, index);
   }
 }
